package com.autumn.file.storage.configure;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.autumn.file.storage.StorageClient;
import com.autumn.file.storage.aliyun.AliyunStorageClient;
import com.autumn.file.storage.configure.AutumnStorageProperties.AliyunStorageClientProperties;
import com.autumn.file.storage.configure.AutumnStorageProperties.LocalStorageClientProperties;
import com.autumn.file.storage.local.LocalStorageClient;
import com.autumn.util.ExceptionUtils;
import com.autumn.util.StringUtils;

/**
 * 存储配置
 * 
 * @author 老码农 2019-03-11 00:38:30
 */
@Configuration
@EnableConfigurationProperties({ AutumnStorageProperties.class })
public class AutumnStorageConfiguration {

	private final Log logger = LogFactory.getLog(AutumnStorageConfiguration.class);

	/**
	 * 客户端Bean 阿里云
	 */
	public static final String BEAN_STORAGE_CLEINT_ALIYUN = "aliyunStorageCleint";

	/**
	 * 客户端Bean 本地
	 */
	public static final String BEAN_STORAGE_CLEINT_LOCAL = "localStorageCleint";

	/**
	 * 阿里云 oos
	 * 
	 * @param properties
	 *            属性
	 * @return
	 */
	@Bean(BEAN_STORAGE_CLEINT_ALIYUN)
	@ConditionalOnProperty(name = AliyunStorageClientProperties.BEAN_CONDITIONAL_PROPERTY, havingValue = "true")
	@ConditionalOnMissingBean(AliyunStorageClient.class)
	public StorageClient aliyunStorageClient(AutumnStorageProperties properties) {
		AliyunStorageClientProperties clientProperties = properties.getAliyun();
		AliyunStorageClient client = new AliyunStorageClient(clientProperties.getEndpoint(),
				clientProperties.getDefaultBucketName(), clientProperties.getAccessKeyId(),
				clientProperties.getAccessKeySecret(), clientProperties.toCannedAccessControlList());
		client.setReadBlockSize(clientProperties.getReadBlockSize());
		client.setWriteBlockSize(clientProperties.getWriteBlockSize());
		logger.info("启用文件存储 " + client.getStorageName() + " 默认分区:" + client.getDefaultBucketName() + " 访问根路径 :"
				+ client.getEndpoint());		
		return client;
	}

	/**
	 * 本地文件存储
	 * 
	 * @param properties
	 *            属性
	 * @return
	 */
	@Bean(BEAN_STORAGE_CLEINT_LOCAL)
	@ConditionalOnProperty(name = LocalStorageClientProperties.BEAN_CONDITIONAL_PROPERTY, havingValue = "true")
	@ConditionalOnMissingBean(LocalStorageClient.class)
	public StorageClient localStorageClient(AutumnStorageProperties properties) {
		LocalStorageClientProperties clientProperties = properties.getLocal();
		String endpoint = clientProperties.getEndpoint();
		if (StringUtils.isNullOrBlank(endpoint)) {
			endpoint = LocalStorageClientProperties.DEFAULT_ENDPOINT;
		}
		String defaultBucketName = clientProperties.getDefaultBucketName();
		if (StringUtils.isNullOrBlank(defaultBucketName)) {
			defaultBucketName = LocalStorageClientProperties.DEFAULT_BUCKET_NAME;
		}
		String rootFilePath = clientProperties.getRootFilePath();
		if (StringUtils.isNullOrBlank(rootFilePath)) {
			rootFilePath = LocalStorageClient.getDefaultLocalPath();
		}
		if (StringUtils.isNullOrBlank(rootFilePath)) {
			ExceptionUtils.throwConfigureException("本地文件存储无默认保存路径 rootFilePath。");
		}
		LocalStorageClient client = new LocalStorageClient(endpoint, defaultBucketName, rootFilePath);
		client.setReadBlockSize(clientProperties.getReadBlockSize());
		client.setWriteBlockSize(clientProperties.getWriteBlockSize());
		logger.info("启用文件存储 " + client.getStorageName() + " 默认分区:" + client.getDefaultBucketName() + " 访问根路径:"
				+ client.getEndpoint() + " 文件保存根路径:" + client.getRootFilePath());
		return client;
	}

}
